﻿using System;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using REVISIONMAX30.Frame;
using REVISIONMAX30.Serialization;
using REVISIONMAX30.DataObjects;

namespace REVISIONMAX30
{
    public class UIHandler
    {
        public delegate void DelegateAddResult(FrResultType pFrResult);

        public DelegateAddResult AddResult;

        public DockPanel dockPanel = null;
        public frmGroupList groupPanel = new frmGroupList();
        public frmResultList resultPanel = new frmResultList();

        public UIHandler(DockPanel pDockPanel)
        {
            this.dockPanel = pDockPanel;
        }

        ~UIHandler()
        {
            this.groupPanel.Close();
            this.groupPanel.Dispose();
            this.groupPanel = null;
            this.dockPanel.Dispose();
            this.dockPanel = null;
        }

        public void InitGroupPanel(DataMain pDataMain)
        {
            this.groupPanel.MdiParent = this.ParentForm;
            this.groupPanel.Initzialize(this, pDataMain);
            this.groupPanel.Show(this.dockPanel);

            this.groupPanel.DockState = DockState.DockLeft;
            this.groupPanel.VisibleState = DockState.DockLeft;


        }

        public void InitResultPanel()
        {
            this.resultPanel.MdiParent = this.ParentForm;
            this.resultPanel.Show(this.dockPanel);
            //this.resultPanel.uiHandler = this;
            this.resultPanel.DockState = DockState.DockBottom;
            this.resultPanel.VisibleState = DockState.DockBottom;

            this.AddResult += new DelegateAddResult(this.resultPanel.AddResult);
        }

        public string ShowHideGroups()
        {
            if (this.groupPanel.Visible)
            {
                this.groupPanel.Hide();
            }
            else
            {
                this.groupPanel.Show();
            }

            return this.groupPanel.Visible ? "Hide Groups Pane" : "Show Groups Pane";
        }

        public string ShowHideResults()
        {
            if (this.resultPanel.Visible)
            {
                this.resultPanel.Hide();
            }
            else
            {
                this.resultPanel.Show();
            }

            return this.resultPanel.Visible ? "Hide Results Pane" : "Show Results Pane";
        }

        public void ViewConnectionString(DataObjects.ConnectionString pConnectionString, DataObjects.Group pGroup)
        {
            new frmConnectionString().View(this, pConnectionString, pGroup);
        }

        public void ViewGroup(DataObjects.Group pGroup, DataObjects.DataMain pParent)
        {
            frmGroup tFrmGroup = null;

            foreach (var tDockPane in this.dockPanel.Panes)
            {
                foreach (var tDockContent in tDockPane.Contents)
                {
                    if ((tDockContent is frmGroup) && (((frmGroup)tDockContent).DataObjectGUID == pGroup.DataObjectGUID))
                    {
                        tFrmGroup = (frmGroup)tDockContent;
                        tFrmGroup.BringToFront();
                        break;
                    }
                }
            }

            if (tFrmGroup == null)
            {
                tFrmGroup = new frmGroup();
                tFrmGroup.MdiParent = this.ParentForm;
                tFrmGroup.Show(this.dockPanel);
                tFrmGroup.DockState = DockState.Document;
                tFrmGroup.VisibleState = DockState.Document;
                tFrmGroup.View(this, pGroup, pParent);
            }
        }

        public void ViewSourceDirectory(DataObjects.SourceDirectory pSourceDirectory, DataObjects.Group pGroup)
        {
            new frmSourceDirectory().View(this, pSourceDirectory, pGroup);
        }

        public void ViewSQLItem(DataObjects.SQLItem pSQLItem, DataObjects.Group pGroup)
        {
            new frmSQLItem().View(this, pSQLItem, pGroup);
        }

        public Form ParentForm
        {
            get
            {
                return (Form)this.dockPanel.Parent;
            }
        }
    }
}

